package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.collection.CollUtil;
import com.zzyl.common.constant.Constants;
import com.zzyl.common.core.redis.RedisCache;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.vo.NursingProjectVo;
import io.lettuce.core.RedisClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingProjectMapper;
import com.zzyl.nursing.domain.NursingProject;
import com.zzyl.nursing.service.INursingProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 护理项目Service业务层处理
 *
 * @author alexis
 * @date 2024-12-30
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询护理项目
     *
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id) {
        return nursingProjectMapper.selectById(id);
    }

    /**
     * 查询护理项目列表
     *
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject) {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject) {
        try {
            redisCache.deleteObject(Constants.CACHE_NURSING_PROJECT_ALL);
        } catch (Exception e) {
            log.error("新增护理项目时删除Redis缓存失败.");
        }
        return nursingProjectMapper.insert(nursingProject);
    }

    /**
     * 修改护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject) {
        try {
            redisCache.deleteObject(Constants.CACHE_NURSING_PROJECT_ALL);
        } catch (Exception e) {
            log.error("修改护理项目时删除Redis缓存失败.");
        }
        return nursingProjectMapper.updateById(nursingProject);
    }

    /**
     * 批量删除护理项目
     *
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids) {
        try {
            redisCache.deleteObject(Constants.CACHE_NURSING_PROJECT_ALL);
        } catch (Exception e) {
            log.error("删除护理项目时删除Redis缓存失败.");
        }
        return nursingProjectMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除护理项目信息
     *
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id) {
        try {
            redisCache.deleteObject(Constants.CACHE_NURSING_PROJECT_ALL);
        } catch (Exception e) {
            log.error("删除护理项目时删除Redis缓存失败.");
        }
        return nursingProjectMapper.deleteById(id);
    }

    /**
     * 查询所有护理项目VO列表
     *
     * @return 结果
     */
    @Override
    public List<NursingProjectVo> getAllProjects() {
        List<NursingProjectVo> list = null;
        //先尝试从Redis中读取, 如果有则返回, 没有则查询
        list = redisCache.getCacheList(Constants.CACHE_NURSING_PROJECT_ALL);
        if (CollUtil.isNotEmpty(list)) {
            return list;
        }
        //Redis没有缓存, 查询数据库, 然后写入缓存
        list = nursingProjectMapper.getAllProjects();
        try {
            redisCache.setCacheList(Constants.CACHE_NURSING_PROJECT_ALL, list);
            redisCache.expire(Constants.CACHE_NURSING_PROJECT_ALL, 24 * 60 * 60, TimeUnit.SECONDS);
        } catch (Exception e) {
            log.error("查询护理项目时写入Redis缓存失败.");
        }
        return list;
    }
}
